var weeklyList;
$(document).ready(function () {
    weeklyList = $('#weeklyList');
    $('.xh-datagrid-toolbar').parseToolbar();
    weeklyList.initDataGrid(baseUrl + 'school_week/getDg',{queryParams:getParams()}).setSelectThenEnable(['bt_list_del']);

    $('#bt_list_search').click(function () {
        weeklyList.addDataGridParams(getParams());
    });


    // $('#select_schoolId').combobox({ //开启可编辑搜素模式
    //     editable:true,
    //     filter: function(q, row){
    //         var opts = $(this).combobox('options');
    //         return row[opts.textField].indexOf(q) >= 0;
    //     }
    // });

    $('#list_search_area').combobox({  //区域选择
        onSelect:function (rec) {
            var pid = rec.value;
            $('#list_search_city').combobox({
                url:baseUrl+'school_examine/getDeps/'+pid,
                valueField: 'id',
                textField: 'text'
            });
        }
    });

    if ($('#bt_list_levelMenu').length > 0) {
        var  $btn_menu2=$('#bt_list_levelMenu').menubutton({
            iconCls: 'fa fa-bars',
            menu: '#mm2'
        });

        $($btn_menu2.menubutton('options').menu).menu({
            onClick: function (item) {
                //item 的相关属性参见API中的menu
                var iSWId = weeklyList.getDataGridSelectedField('iSWId');
                if ( iSWId  == '') {
                    window.top.xhTop.showError('错误' , '请先选择一个周报');
                    return false;
                }
                if (item.text == '优秀') {
                    xhExtra.askChangeStatus('school_week/changeLevel',{id:iSWId,level:3},null,'确认要将该周报评为优秀周报吗？',function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                        weeklyList.dataGridReload();
                    })
                } else if(item.text == '良好'){
                    xhExtra.askChangeStatus('school_week/changeLevel',{id:iSWId,level:2},null,'确认要将该周报评为良好周报吗？',function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                        weeklyList.dataGridReload();
                    })
                } else if (item.text == '一般') {
                    xhExtra.askChangeStatus('school_week/changeLevel',{id:iSWId,level:1},null,'确认要将该周报评为一般周报吗？',function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                        weeklyList.dataGridReload();
                    })
                }
            }
        });
    }


    var  $btn_menu1=$('#bt_list_exmenu').menubutton({
        iconCls: 'fa fa-bars',
        menu: '#mm'
    });
    $($btn_menu1.menubutton('options').menu).menu({
        onClick: function (item) {
            //item 的相关属性参见API中的menu
            // var schoolId = weeklyList.getDataGridSelectedField('iSchoolId');
            var schoolIds = weeklyList.getDataGridSelectedFields('iSchoolId',true);
            if (schoolIds.length !== 1) {
                window.top.xhTop.showError('错误' , '请先选择一个学校');
                return false;
            }
            if(item.text == '周报打分'){
                window.top.xhTop.showModal({
                    title: '周报打分',
                    content: '<iframe  src="'+baseUrl+'school_week/weekGrade?schoolId='+schoolIds[0]+'"  style="width: 100%;;border-style: none;padding: 0px;height:300px"  scrolling="yes"   ></iframe>',
                    button:'提交',
                    backClose:false
                });
            }else if(item.text == '查看周报打分'){
                window.top.xhTop.showModal({
                    title: '查看周报打分',
                    content: '<iframe  src="'+baseUrl+'school_week/gradeList?schoolId='+schoolIds[0]+'"  style="width: 100%;;border-style: none;padding: 0px;height:300px"  scrolling="yes"   ></iframe>',
                    backClose:false
                });
            }
        }
    });

    DataGrid.onLoadSuccess = function () {
        weeklyList.dataGridClear();
    };

    DataGrid.onDblClickRow = function (rowIndex, rowData) {
        window.top.xhTop.createNewTab(
            '周报-' + rowData['sSchoolName'],
            baseUrl + 'school_week/weekly?iSWId=' + rowData['iSWId'],
            true
        );
    };

    $('#bt_list_del').click(function () {
        var ids = weeklyList.getDataGridSelectedFields('iSWId');
        myWindow.initPopUp({title: '确定要删除么?',text: '这些周报将被删除!'},function () {
            $.ajax({
                url:baseUrl+'school_week/delWeekly',
                data:{ids:ids},
                type:'POST',
                success:function (data) {
                    window.top.swal(
                        data['msg'],
                        '',
                        'success'
                    );
                    weeklyList.dataGridReload();
                },
                error:function (data) {
                    window.top.swal(
                        data['responseText'],
                        '',
                        'error'
                    );
                    weeklyList.dataGridReload();
                }
            });
        });
    });
    
    
});
function getParams() {
    var key = xhExtra.getValue($('#list_search'));
    var schoolId = xhExtra.getValue($('#select_schoolId'));
    var state    = xhExtra.getValue($('#select_state'));
    var city     = xhExtra.getValue($('#list_search_city'));
    var area     = xhExtra.getValue($('#list_search_area'))
    return {key:key,schoolId:schoolId,state:state,city:city,area:area};
}